package xin.huangyan.accessibilityapp;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.RequiresApi;

import java.util.Random;

import xin.huangyan.accessibilityapp.utils.HLog;

public class WeixinMsgManager {
    private static WeixinMsgManager instance;
    private WeixinMsgManager(){}
    public static WeixinMsgManager getInstance() {
        if (instance == null) {
            synchronized (WeixinMsgManager.class){
                if (instance == null) {
                    instance = new WeixinMsgManager();
                }
            }
        }
        if (accessibilityService == null) {
            accessibilityService = MyAccessibilityService.getService();
        }
        return instance;
    }

    static MyAccessibilityService accessibilityService;
    Handler mainHandler = new Handler(Looper.getMainLooper());
    int actionInterval = 500;
    Random random = new Random();
    //发送微信消息
    public void sendMsg(String weixinMsg){
        //打开微信
        AccessibilityUtil.startWeixinHome(MainApplication.context);
        String idSearch = "com.tencent.mm:id/he6";//首页搜索
        //找搜索框，点击
        accessibilityService.setFindViewCallback(new FindViewCallback(){
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            void result(AccessibilityNodeInfo nodeInfo) {
                HLog.i("找到搜索按钮");
                mainHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        accessibilityService.dispatchTouchGesture(nodeInfo, 300);
                        searchInput(weixinMsg);
                    }
                }, actionInterval);
            }
        }.setId(idSearch));
    }
    //输入搜索内容，搜索联系人
    public void searchInput(String weixinMsg){
        String idSearchInput = "com.tencent.mm:id/bxz";//首页搜索输入框
        //找搜索框，点击
        accessibilityService.setFindViewCallback(new FindViewCallback(){
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            void result(AccessibilityNodeInfo nodeInfo) {
                HLog.i("找到搜索输入框");
                mainHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        accessibilityService.inputText(nodeInfo, "Yanxin");
                        openMsgPage(weixinMsg);
                    }
                }, actionInterval);
            }
        }.setId(idSearchInput));
    }
    //打开联系人聊天页面
    public void openMsgPage(String weixinMsg){
//        String idMsgItem = "com.tencent.mm:id/hee";
        String idMsgItem = "com.tencent.mm:id/x1";
        accessibilityService.setFindViewCallback(new FindViewCallback(){
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            void result(AccessibilityNodeInfo nodeInfo) {
                HLog.i("找到Item");
                mainHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        accessibilityService.dispatchTouchGesture(nodeInfo, 300);
                        inputMsg(weixinMsg);
                    }
                }, actionInterval);
            }
        }.setId(idMsgItem));
    }
    //点击输入框
    public void inputMsg(String weixinMsg){
        String idMsgInput = "com.tencent.mm:id/auj";
        accessibilityService.setFindViewCallback(new FindViewCallback(){
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            void result(AccessibilityNodeInfo nodeInfo) {
                HLog.i("找到输入框");
                mainHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        HLog.i("输入内容");
                        if (TextUtils.isEmpty(weixinMsg)) {
                            accessibilityService.inputText(nodeInfo, "这是一条无障碍消息" + random.nextDouble());
                        }else {
                            accessibilityService.inputText(nodeInfo, weixinMsg);
                        }
                        actionSendBtn();
                    }
                }, actionInterval);
            }
        }.setId(idMsgInput).setClassName("android.widget.EditText"));
    }
    //点击发送按钮
    public void actionSendBtn(){
        String idSendBtn = "com.tencent.mm:id/ay5";
        accessibilityService.setFindViewCallback(new FindViewCallback(){
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            void result(AccessibilityNodeInfo nodeInfo) {
                HLog.i("发送按钮");
                mainHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        accessibilityService.dispatchTouchGesture(nodeInfo, 300);
                        backClick1();
                    }
                }, actionInterval);
            }
        }.setId(idSendBtn));
    }
    //点三次返回按钮，回到原应用，等待下一条消息
    public void backClick1(){
        mainHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                accessibilityService.performBackClick();
                backClick2();
            }
        }, actionInterval);
    }
    public void backClick2(){
        mainHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                accessibilityService.performBackClick();
                backClick3();
            }
        }, actionInterval);
    }
    public void backClick3(){
        mainHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                accessibilityService.performBackClick();
            }
        }, actionInterval);
    }

}
